import {MessageBox} from "element-ui";
import CryptoJS from "crypto-js"
const secret = "eqweqwesasaq"
export default {
    async deleteConfirm(message) {
        return new Promise(resolve => {
            MessageBox.confirm(message ? message : '此操作将永久删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                resolve(true)
            }).catch(() => {
                resolve(false)
            })
        })
    },
    /*
    * arr 数组
    * fled 需要判断的字段
    * */
    isUnique(arr, fled) {
        let ret = arr.sort((a, b) => {
            return a[fled].localeCompare(b[fled]);                                          //localeCompare 字符串比较
        })
        for (var i = 1; i < ret.length; i++) {
            if (ret[i][fled] == ret[i - 1][fled]) {
                return false
            }
        }
        return true
    },
    sort(arr, fled) {
        return arr.sort((a, b) => {
            return a[fled].localeCompare(b[fled]);                                          //localeCompare 字符串比较
        })
    },

    encrypt(word) { // 加密
        let key = CryptoJS.enc.Utf8.parse(secret);
        let srcs = CryptoJS.enc.Utf8.parse(word);
        let encrypted = CryptoJS.AES.encrypt(srcs, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return encrypted.toString();
    },
    decrypt(word) { // 解密
        let key = CryptoJS.enc.Utf8.parse(secret);
        let decrypt = CryptoJS.AES.decrypt(word, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return CryptoJS.enc.Utf8.stringify(decrypt).toString();
    },

}